package com.movie.service.impl;

import com.movie.dao.OrderMapper;
import com.movie.model.Order;
import com.movie.model.OrderSeat;
import com.movie.service.OrderService;
import com.movie.util.OrderRequest;
import com.movie.util.SeatInfo;
import com.movie.util.SeatRequest;
import com.movie.util.OrderStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    @Transactional
    public Integer createOrder(Integer userId, OrderRequest request) {
        // 检查座位是否可用

        System.out.println(request);

        for (SeatInfo seat : request.getSeats()) {
            if (!orderMapper.checkSeatAvailability(
                    request.getScreeningId(), seat.getRow(), seat.getColumn())) {
                throw new RuntimeException("所选座位已被占用");
            }
        }

        // 创建订单
        Order order = new Order();
        order.setUserId(userId);
        order.setScreeningId(request.getScreeningId());
        order.setTotalAmount(request.getTotalAmount());
        order.setPaymentStatus("UNPAID");

        // 生成二维码内容
        String qrContent = generateQRContent(request);
        order.setQrCode(qrContent);

        // 保存订单
        // 在保存订单后返回订单ID
        orderMapper.createOrder(order);
        return order.getId();
    }

    @Override
    public List<Order> getUserOrders(Integer userId) {
        List<Order> orders = orderMapper.getOrdersByUserId(userId);

        for (Order order : orders) {
            System.out.println(order);
            order.setSeats(orderMapper.getOrderSeats(order.getId()));
        }
        return orders;
    }

    @Override
    public Order getOrderDetails(Integer orderId) {
        Order order = orderMapper.getOrderById(orderId);
        if (order != null) {
            order.setSeats(orderMapper.getOrderSeats(orderId));
        }
        return order;
    }

    @Override
    @Transactional
    public boolean processPayment(Integer orderId) {
        try {
            orderMapper.updateOrderStatus(orderId, "PAID");
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean checkSeatAvailability(SeatRequest request) {
        return orderMapper.checkSeatAvailability(
                request.getScreeningId(),
                request.getRow(),
                request.getColumn()
        );
    }

    @Override
    public List<OrderSeat> getScreeningSeats(Integer screeningId) {
        return orderMapper.getScreeningSeats(screeningId);
    }

    private String generateQRContent(OrderRequest request) {
        // 生成唯一的二维码内容
        return UUID.randomUUID().toString();
    }

    @Override
    public OrderStatistics getOrderStatistics() {
        OrderStatistics statistics = new OrderStatistics();
        
        // 获取各项统计数据
        statistics.setTotalOrders(orderMapper.getTotalOrderCount());
        statistics.setPaidOrders(orderMapper.getPaidOrderCount());
        statistics.setUnpaidOrders(orderMapper.getUnpaidOrderCount());
        statistics.setTotalRevenue(orderMapper.getTotalRevenue());
        statistics.setTodayRevenue(orderMapper.getTodayRevenue());
        statistics.setTodayOrders(orderMapper.getTodayOrderCount());
        statistics.setTotalUsers(orderMapper.getTotalUserCount());
        statistics.setTotalSeats(orderMapper.getTotalSeatCount());
        
        return statistics;
    }
}